﻿using System.Drawing;
using System.IO;

namespace Utility
{
    public class Img
    {
        public static int New_width, New_height;
        public Img()
        {
        }
        #region 格式化照片的大小，返回Image对象
        /// <summary>
        /// 格式化照片的大小，返回Image对象
        /// </summary>
        /// <param name="ImgStream">照片的字节流</param>
        /// <param name="width">新宽</param>
        /// <param name="height">新高</param>
        /// <param name="moder">设置的模式（W：指定宽 H：指定高 WH：指定宽高 Cut：指定宽高，剪切）</param>
        /// <returns></returns>
        public static System.Drawing.Image FormatImg(Stream ImgStream, int width, int height, string moder)
        {
            System.Drawing.Image DrImg = System.Drawing.Image.FromStream(ImgStream);
            New_width = width;
            New_height = height;
            int Old_width = DrImg.Width;
            int Old_height = DrImg.Height;
            int x = 0;
            int y = 0;
            switch (moder)
            {
                case "WH":  //指定宽高（可能变形）
                    break;
                case "W":   //指定宽
                    if (Old_width < New_width) { New_width = Old_width; }
                    New_height = Old_height * New_width / Old_width;
                    break;
                case "H":   //指定高
                    if (Old_height < New_height) { New_height = Old_height; }
                    New_width = Old_width * New_height / Old_height;
                    break;
                case "Cut": //指定宽高，剪切
                    break;
            }
            //新建Bitmap图片
            System.Drawing.Image BitMap = new System.Drawing.Bitmap(New_width, New_height);
            //新建一个画板
            System.Drawing.Graphics Grap = System.Drawing.Graphics.FromImage(BitMap);
            //设置高质量插值法
            Grap.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            //设置高质量、低速度呈现平滑度
            Grap.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //清空画布并以透明背景色填充
            Grap.Clear(System.Drawing.Color.Transparent);
            //在指定位置并且按指定大小绘制原图片的指定部分
            Grap.DrawImage(DrImg, new System.Drawing.Rectangle(0, 0, New_width, New_height), new System.Drawing.Rectangle(x, y, Old_width, Old_height), System.Drawing.GraphicsUnit.Pixel);
            DrImg.Dispose();
            Grap.Dispose();
            return BitMap;
        }
        #endregion

        public static Bitmap ResetImg(string ImgFile, int PicWidth, int PicHeight, int PointX, int PointY, int CutWidth, int CutHeight)
        {
            int sjw, sjh, new_width, new_height;

            string getpicpath = System.Web.HttpContext.Current.Server.MapPath(ImgFile);
            System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(getpicpath);
            //if (imgPhoto.Width > PicWidth)
            //    sjw = PicWidth;
            //else
            //    sjw = imgPhoto.Width;
            //if (imgPhoto.Height > PicHeight)
            //    sjh = PicHeight;
            //else
            //    sjh = imgPhoto.Height;
            sjw = PicWidth;
            sjh = PicHeight;
            if (CutWidth > 160)
                new_width = 160;
            else
                new_width = CutWidth;
            if (CutHeight > 190)
                new_height = 190;
            else
                new_height = CutHeight;
            Bitmap bmPhoto = new Bitmap(new_width, new_height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            Graphics gbmPhoto = Graphics.FromImage(bmPhoto);
            gbmPhoto.Clear(Color.White);
            gbmPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, new_width, new_height), PointX * imgPhoto.Width / sjw, PointY * imgPhoto.Height / sjh, CutWidth * imgPhoto.Width / sjw, CutHeight * imgPhoto.Height / sjh, GraphicsUnit.Pixel);
            gbmPhoto.Dispose();
            imgPhoto.Dispose();
            return bmPhoto;
        }
    }
}
